package com.work.yly.website.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.work.yly.website.service.ServiceDefine;
import com.work.yly.website.service.ServiceHelper;
import com.work.yly.website.vo.Lccp;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/finance")
public class FinanceController extends BaseController{
	
	@RequestMapping("/index")
	public String index(Model model) {		
		JSONObject ret = ServiceHelper.lccplist();
		if(ret.getInt("flag")==ServiceDefine.CALL_SUCCESS){
			JSONArray data = ret.getJSONObject("data").getJSONArray("lccp_list");
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
			List<Lccp> list = Lists.newArrayList();
			for(int i=0; i<data.size();i++){
				Lccp lccp = gson.fromJson(data.getString(i), Lccp.class);
				list.add(lccp);
			}
			model.addAttribute("lccp_list", list);
		}
		else{
			model.addAttribute("error", ret.getString("message"));
		}
		return "/financing/index";
	}
	
	@RequestMapping("/lc_detail")
	public String detail(HttpServletRequest request, Model model) {
		String lccp_id = request.getParameter("lccp_id");
		HttpSession session = request.getSession();
		Object hy_id = session.getAttribute("hy_id");
		if (null == hy_id) {
			return "redirect:../login?successUrl=/finance/lc_detail?lccp_id="+lccp_id;
		}
	
		Object zh_id = session.getAttribute("zh_id");
		JSONObject ret = ServiceHelper.getZlWsqk(Integer.valueOf(zh_id.toString()));
		if(ret.getInt("flag")==ServiceDefine.CALL_SUCCESS){
			if(!ret.getJSONObject("data").getBoolean("allBas")){
				return "redirect:../my/hyxx?dojk=2&lccp_id="+lccp_id;
			}
		}
		ret = ServiceHelper.lccplist();
		if(ret.getInt("flag")==ServiceDefine.CALL_SUCCESS){
			JSONArray data = ret.getJSONObject("data").getJSONArray("lccp_list");
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
			Lccp lccp = null;
			for(int i=0; i<data.size();i++){
				lccp = gson.fromJson(data.getString(i), Lccp.class);
				if((""+lccp.getId()).equals(lccp_id)){
					break;
				}
			}
			model.addAttribute("lccp", lccp);
			JSONObject obj = ServiceHelper.getMyWealth(Integer.valueOf(hy_id.toString()));
			if (obj.getInt("flag") == ServiceDefine.CALL_SUCCESS) {
				JSONObject data1 = obj.getJSONObject("data");
				model.addAttribute("fcbje", data1.getInt("fcbje"));
			}
		}
		else{
			model.addAttribute("error", ret.getString("message"));
		}
		return "/financing/lc_detail";
	}
	
	@RequestMapping("/saveorder")
	public String order(HttpServletRequest request, Model model) {
		String id = request.getParameter("lccp_id");
		String hy_id = request.getParameter("hy_id");
		String gmje = request.getParameter("gmje");
		String fcbje = request.getParameter("fcbje");
		if(StringUtils.isBlank(fcbje)){
			fcbje="0";
		}
			
		JSONObject ret = ServiceHelper.saveLcOrder(Integer.valueOf(hy_id), Integer.valueOf(id), Double.valueOf(gmje), Integer.valueOf(fcbje));
		if(ret.getInt("flag")==ServiceDefine.CALL_SUCCESS){
			model.addAttribute("error", ret.getString("message"));
			return "redirect:/finance/index";
		}
		else{
			model.addAttribute("error", ret.getString("message"));
			return detail(request, model);
		}
		
	}
	
	@RequestMapping("/order")
	public String pay(HttpServletRequest request, Model model){
		model.addAttribute("lccp_id", request.getParameter("lccp_id"));
		model.addAttribute("cpmc", request.getParameter("cpmc"));
		model.addAttribute("syl", request.getParameter("syl"));
		model.addAttribute("fbq", request.getParameter("fbq"));
		model.addAttribute("gmje", request.getParameter("gmje"));
		model.addAttribute("sxje", request.getParameter("sxje"));
		model.addAttribute("sxbl", request.getParameter("sxbl"));
		model.addAttribute("kyfcbje", request.getParameter("kyfcbje"));
		model.addAttribute("qtje", request.getParameter("qtje"));
		
		return "/financing/order"; 
	}
}
